package com.peanut.youfupay.controller;


import com.alibaba.fastjson.JSON;
import com.peanut.youfupay.entity.response.ResponseMessage;
import com.peanut.youfupay.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 优付签约结果异步通知
 * 1、签约只有成功才会进行异步通知
 * 2、通知逻辑：累计20次，商户返回SUCCESS则停止
 * 每隔10秒通知1次，共1次
 * 每隔5秒通知1次，共1次
 * 每隔10秒通知1次，共3次
 * 每隔30秒通知1次，共4次
 * 每隔1分钟通知1次，共6次
 * 每隔5分钟通知1次，共5次
 *
 * @author peanut
 */
@Controller
@RequestMapping("/sign")
@Slf4j
public class SignNotifyController {
    @Autowired
    BaseService baseService;

    @PostMapping(value = "/notify")
    @ResponseBody
    public String doBusiness(@RequestBody String encryptedMsg) {

        log.info("优付签约异步通知返回报文:" + encryptedMsg);
        ResponseMessage encryptedResponseMessage = JSON.parseObject(encryptedMsg, ResponseMessage.class);
        ResponseMessage responseMessage = baseService.handleResponse(encryptedResponseMessage);
        String notifyData = responseMessage.getResData();
        log.info("优付签约异步通知返回结果-notifyData:" + notifyData);
        //返回给优付的结果
        return "SUCCESS";
    }

    /**优付签约异步通知返回完整报文
     * 商户可用此报文作为请求报文用postMan发给异步通知controller测试
     * Content-Type=application/json
     * /

    /**
     {"funCode":"6011","merId":"89900000300716159604","resCode":"0000",
     "resData":"4OaStBQd+o8DvbQY+bzzgHjmlV3QM5sBXP1Gjn66Y8dTKbXVckqaYzU5JZ5muxckkwQplGDf2oYGI1UguaqHdu8P1/j5wxpbUewiACzDVBdnwX0C2AzFGLcyW682+eVoBzhrOKAWBaoUi4xfVjiWf5Xyv0uqfhPX3vxA3INFJ0TDXjk1KF+UOVl+V6dEtEwq2ZPGR+2kZDo=",
     "resMsg":"成功","sign":"Et0W2toP2AxZlROCnUPr63rYhXN2RKzKJydDiZ7YG6o5wqt+ujyVUWllrW/5kOCE21tN5nJQPZiid4eiC+9YHJw2WAxF4UssZy5hfW0bbAWcFzmDPZW2yLYUS+T0wWI7zKnEkZxcZmDR28I4EkNgW85It2Gss+I0n2Siciq35TU=","version":"V1.0"}
     */

}
